import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/mrp_bom_odoo_api.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/models/odoo/mrp_bom.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import '../../../generated/l10n.dart';

class MrpBomEdit extends StatefulWidget {
  MrpBomEdit({this.mrpBom});

  final MrpBom? mrpBom;

  @override
  _MrpBomEditState createState() => _MrpBomEditState();
}

class _MrpBomEditState extends State<MrpBomEdit> {
  late MrpBom mrpBom;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  //late List<SelectOptionVO> activeList;


  @override
  void initState() {
    super.initState();
    this.isAdd = widget.mrpBom == null;
    this.mrpBom = widget.mrpBom ?? MrpBom();
    _loadSelectVO();
  }

   _loadSelectVO() async{
    //activeList=[SelectOptionVO(value:'0',label: "禁用"),SelectOptionVO(value:'1',label: "启用")];
    setState(() { });
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInput(
            label: 'message_main_attachment_id',
            value: mrpBom.messageMainAttachmentId,
            onSaved: (v) {
              mrpBom.messageMainAttachmentId = v;
            },
          ),

          CryInput(
            label: 'BOM清单编码',
            value: mrpBom.code,
            onSaved: (v) {
              mrpBom.code = v;
            },
          ),

          CryInput(
            label: 'active',
            value: mrpBom.active,
            onSaved: (v) {
              mrpBom.active = v;
            },
          ),

          CryInput(
            label: 'type',
            value: mrpBom.type,
            onSaved: (v) {
              mrpBom.type = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),

          CryInput(
            label: '产品id',
            value: mrpBom.productTmplId,
            onSaved: (v) {
              mrpBom.productTmplId = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),

          CryInput(
            label: 'product_id',
            value: mrpBom.productId,
            onSaved: (v) {
              mrpBom.productId = v;
            },
          ),

          CryInput(
            label: 'product_qty',
            value: mrpBom.productQty,
            onSaved: (v) {
              mrpBom.productQty = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),

          CryInput(
            label: 'product_uom_id',
            value: mrpBom.productUomId,
            onSaved: (v) {
              mrpBom.productUomId = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),

          CryInput(
            label: 'sequence',
            value: mrpBom.sequence,
            onSaved: (v) {
              mrpBom.sequence = v;
            },
          ),

          CryInput(
            label: 'ready_to_produce',
            value: mrpBom.readyToProduce,
            onSaved: (v) {
              mrpBom.readyToProduce = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),

          CryInput(
            label: 'picking_type_id',
            value: mrpBom.pickingTypeId,
            onSaved: (v) {
              mrpBom.pickingTypeId = v;
            },
          ),

          CryInput(
            label: 'company_id',
            value: mrpBom.companyId,
            onSaved: (v) {
              mrpBom.companyId = v;
            },
          ),

          CryInput(
            label: 'consumption',
            value: mrpBom.consumption,
            onSaved: (v) {
              mrpBom.consumption = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),

          CryInput(
            label: 'create_uid',
            value: mrpBom.createUid,
            onSaved: (v) {
              mrpBom.createUid = v;
            },
          ),

          CryInput(
            label: 'write_uid',
            value: mrpBom.writeUid,
            onSaved: (v) {
              mrpBom.writeUid = v;
            },
          ),

           CrySelectDate(
            context,
            label: 'write_date',
            value: mrpBom.writeDate,
            onSaved: (v) {
              mrpBom.writeDate = v;
            },
          ),

        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            MrpBomOdooApi.saveOrUpdate(mrpBom.toOdooMap()).then((ResponseBodyApi res) {
              if (!res.success!) {
               //CryUtils.message(re s.message!);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd! ? S.of(context).add : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}